<?php

namespace Ignus\View\TokenParser;

use Ignus\View\Error\Syntax;
use Ignus\View\TokenParser;
use Ignus\View\Token;

class ExtendsParser extends TokenParser
{
    public function parse(Token $token)
    {
        if (!$this->parser->isMainScope()) {
            throw new Syntax('Cannot extend from a block', $token->getLine());
        }

        if (null !== $this->parser->getParent()) {
            throw new \Ignus\View\Error\Syntax('Multiple extends tags are forbidden', $token->getLine());
        }
        $this->parser->setParent($this->parser->getExpressionParser()->parseExpression());

        $this->parser->getStream()->expect(Token::BLOCK_END_TYPE);

        return null;
    }

    public function getTag()
    {
        return 'extends';
    }
}
